package uit.bus;

import java.util.ArrayList;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.CheckBox;

public class KetQuaTimKiem extends ListActivity {
	public ArrayList<OrderKQTK> listOrder = null;
	public OrderKQTKAdapter adapter;
	public DBAdapter db;
	public static String noiDi;
	public static String noiDen;
	public ArrayList<Integer> list1tuyen = new ArrayList<Integer>();
	public ArrayList<OrderHaiTuyen> list2tuyen = new ArrayList<OrderHaiTuyen>();
	public OrderHaiTuyen order2t;
	CheckBox cbXemtk;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listkqtimkiem);
		listOrder = new ArrayList<OrderKQTK>();
		adapter = new OrderKQTKAdapter(this, R.layout.listkqtimkiem, listOrder);
		setListAdapter(adapter);
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			noiDi = extras.getString("noidi");
			noiDen = extras.getString("noiden");
			tim1tuyen();
			if (list1tuyen.size() < 1) {
				tim2tuyen();
			}
			getListOrder();
		}
	}

	public void tim1tuyen() {
		db = new DBAdapter(this);
		db.open();
		Cursor cursor1Di = db.timtuyen(noiDi);
		Cursor cursor1Den = db.timtuyen(noiDen);
		int[] list1Di = new int[cursor1Di.getCount()];
		int[] list1Den = new int[cursor1Den.getCount()];
		int index = 0;
		for (cursor1Di.moveToFirst(); !cursor1Di.isAfterLast(); cursor1Di
				.moveToNext()) {
			list1Di[index] = cursor1Di.getInt(0);
			index++;
		}
		index = 0;
		for (cursor1Den.moveToFirst(); !cursor1Den.isAfterLast(); cursor1Den
				.moveToNext()) {
			list1Den[index] = cursor1Den.getInt(0);
			index++;
		}
		for (int i = 0; i < list1Di.length; i++)
			for (int j = 0; j < list1Den.length; j++) {
				if (list1Di[i] == list1Den[j]) {
					list1tuyen.add(new Integer(list1Di[i]));
				}
			}
		// for (int i = 0; i < list1tuyen.size(); i++) {
		// Toast.makeText(
		// this,
		// "ma tuyen =" + String.valueOf(list1tuyen.get(i).intValue()),
		// Toast.LENGTH_LONG).show();
		// }
		db.close();
	}

	public void tim2tuyen() {
		db = new DBAdapter(this);
		db.open();
		Cursor cursor2Di = db.timtuyen(noiDi);
		Cursor cursor2Den = db.timtuyen(noiDen);
		int[] list2Di = new int[cursor2Di.getCount()];
		int[] list2Den = new int[cursor2Den.getCount()];
		int index = 0;
		for (cursor2Di.moveToFirst(); !cursor2Di.isAfterLast(); cursor2Di
				.moveToNext()) {
			list2Di[index] = cursor2Di.getInt(0);
			index++;
		}
		index = 0;
		for (cursor2Den.moveToFirst(); !cursor2Den.isAfterLast(); cursor2Den
				.moveToNext()) {
			list2Den[index] = cursor2Den.getInt(0);
			index++;
		}
		// loai bo cac ma tuyen trung
		for (int i = 0; i < list2Di.length; i++)
			for (int j = 0; j < list2Den.length; j++) {
				if (list2Di[i] != list2Den[j]) {
					order2t = new OrderHaiTuyen();
					order2t.setMatuyen1(new Integer(list2Di[i]));
					order2t.setMatuyen2(new Integer(list2Den[j]));
					list2tuyen.add(order2t);
				}
			}
	}

	public void getListOrder() {
		OrderKQTK order;
		listOrder = new ArrayList<OrderKQTK>();
		db = new DBAdapter(this);
		db.open();
		Cursor myCursor;
		for (int i = 0; i < list1tuyen.size(); i++) {
			myCursor = db.getTuyen(list1tuyen.get(i).intValue());
			order = new OrderKQTK();
			if (myCursor.moveToFirst()) {
				order.setTrangthai(1);
				order.setMatuyen1(myCursor.getInt(0));
				order.setTentuyen1(myCursor.getString(1));
				listOrder.add(order);
			}
		}
		// xu ly voi list2tuyen
		for (int i = 0; i < list2tuyen.size(); i++) {
			order2t = list2tuyen.get(i);
			myCursor = db.getdiemchuyen(order2t.getMatuyen1(),
					order2t.getMatuyen2());
			for (myCursor.moveToFirst(); !myCursor.isAfterLast(); myCursor
					.moveToNext()) {
				order = new OrderKQTK();
				order.setMatuyen1(order2t.getMatuyen1());
				order.setMatuyen2(order2t.getMatuyen2());
				order.setDiemchuyen(myCursor.getString(2));
				listOrder.add(order);
			}
		}
		db.close();
		if (listOrder != null && listOrder.size() > 0) {
			for (int i = 0; i < listOrder.size(); i++) {
				adapter.add(listOrder.get(i));
			}
		}
		adapter.notifyDataSetChanged();
	}
}
